package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.ServiceOrder;
import com.lovo.service.ServiceOrderService;
import com.lovo.service.impl.ServiceOrderServerImpl;
import com.lovo.VO.ServiceOrderVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServletServiceOrder", urlPatterns = "/ServletServiceOrder.do")
public class ServletServiceOrder extends HttpServlet {
    ServiceOrderService serviceOrderService=new ServiceOrderServerImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        System.out.println("method = " + method);
        if(method.equals("selectServiceAll")){
            selectServiceAll(request,response);
        }
        if(method.equals("delectService")){
            delectService(request,response);
        }
        if(method.equals("updateService")){
            updateService(request,response);
        }
        if(method.equals("selectById")){
            selectById(request,response);
        }
    }

    private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String isId=request.getParameter("id");
        int id=Integer.parseInt(request.getParameter("id"));
        ServiceOrder ov=serviceOrderService.selectByPrimaryKey(id);
        ObjectMapper objectMapper=new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(ov));
    }

    private void updateService(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        ServiceOrder vo=new ServiceOrder();
        vo.setServiceOrderId(Integer.parseInt(request.getParameter("id")));
        vo.setServiceOrderOwner(request.getParameter("serviceOrderOwner"));
        vo.setServiceOrderTel(request.getParameter("serviceOrderTel"));
        int n=serviceOrderService.updateByPrimaryKey(vo);
        if(n>0){
            response.getWriter().write("true");
        }else response.getWriter().write("false");
    }

    private void delectService(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        int n=serviceOrderService.deleteByPrimaryKey(id);
        if(n>0){
            response.getWriter().write("true");
        }else response.getWriter().write("false");
    }


    private void selectServiceAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));
        int pageNo=1;
        if(request.getParameter("pageNo")==null){
            pageNo=0;
        }else{
            pageNo=Integer.parseInt(request.getParameter("pageNo"));
        }
        ServiceOrderVo vo=new ServiceOrderVo();
        vo.setName(request.getParameter("txt"));
        PageInfo<ServiceOrderVo> pageInfo=serviceOrderService.selectAll(vo,pageNo,pageSize);
        ObjectMapper objectMapper = new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(pageInfo));
    }
}
